//
//  SideSlipCommonTableViewCell.m
//  quxiangtao
//
//  Created by xbmios on 2018/8/6.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "SideSlipCommonTableViewCell.h"
#import "FilterCommonCollectionViewCell.h"
#import "CommonItemModel.h"


static NSString *collectionCellID = @"collectionCellID";


@interface SideSlipCommonTableViewCell () <UICollectionViewDelegate, UICollectionViewDataSource>

@property (strong, nonatomic) UILabel *titleL;

@property (strong, nonatomic) UILabel *subTitleL;
@property (strong, nonatomic) UIImageView *arrowIV;
@property (strong, nonatomic) UICollectionView *mainCollectionView;

@property (strong, nonatomic) NSArray *collectionArr;

@property (strong, nonatomic) NSIndexPath *indexPath;
@property (strong, nonatomic) ZYSideSlipFilterRegionModel *regionModel;

@property (strong, nonatomic) NSMutableArray *selectedItemList;
@property (copy, nonatomic) NSString *selectedItemString;
@property (assign, nonatomic) CommonTableViewCellSelectionType selectionType;

@end

@implementation SideSlipCommonTableViewCell

+ (NSString *)cellReuseIdentifier {
    return @"SideSlipCommonTableViewCell";
}

+ (instancetype)createCellWithIndexPath:(NSIndexPath *)indexPath {

    SideSlipCommonTableViewCell *cell = [[NSClassFromString(@"SideSlipCommonTableViewCell") alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SideSlipCommonTableViewCell"] ;
    cell.indexPath = indexPath;
    return cell;
}

- (void)updateCellWithModel:(ZYSideSlipFilterRegionModel **)model
                  indexPath:(NSIndexPath *)indexPath {
    self.indexPath = indexPath;
    self.regionModel = *model;
    //title
    [self.titleL setText:_regionModel.regionTitle];
    //content
    self.collectionArr = _regionModel.itemList;
    //icon
    if (_regionModel.isShowAll) {
        
//        [_arrowIV setImage:[UIImage imageNamed:@"icon_up"]];
    } else {
        
//        [_arrowIV setImage:[UIImage imageNamed:@"icon_down"]];
    }
    //controlLabel
    self.selectedItemList = [NSMutableArray arrayWithArray:_regionModel.selectedItemList];
    [self generateControlLabelText];
    //selectionType
    NSNumber *selectionType = _regionModel.customDict[REGION_SELECTION_TYPE];
    if (selectionType) {
        self.selectionType = [selectionType unsignedIntegerValue];
    }
    //UI
    [_mainCollectionView reloadData];
    [self fitCollectonViewHeight];
    
    CGFloat height = CGRectGetMaxY(self.mainCollectionView.frame) + jisuan(30);
    
//    NSLog(@"%.2f",CGRectGetMaxY(self.mainCollectionView.frame));
    
    if (self.cellHeigthBlock) {
        
        self.cellHeigthBlock(height);
    }
    
}


//根据数据源个数决定collectionView高度
- (void)fitCollectonViewHeight {
    CGFloat displayNumOfRow;
    
    NSInteger count = self.collectionArr.count % 3;
    NSInteger row = self.collectionArr.count / 3;

    if (_regionModel.isShowAll) {
        
        
        if (count == 0) {
            
            displayNumOfRow = row;
            
        }else {
            
            displayNumOfRow = row + 1;
        }
        
        
    } else {
        
        displayNumOfRow = 1;
        
    }
 
    CGFloat collectionViewHeight = displayNumOfRow * jisuan(60) + (displayNumOfRow - 1) * jisuan(20);
    
//    NSLog(@"%@", NSStringFromCGRect(self.mainCollectionView.frame));
    self.mainCollectionView.height = collectionViewHeight;
    [self.mainCollectionView updateHeight:collectionViewHeight];
//    NSLog(@"====%@", NSStringFromCGRect(self.mainCollectionView.frame));
    
}


- (NSString *)packageSelectedNameString {
    NSMutableArray *mutArray = [NSMutableArray array];
    for (CommonItemModel *model in _selectedItemList) {
        [mutArray addObject:model.itemName];
    }
    return [mutArray componentsJoinedByString:@","];
}
- (void)generateControlLabelText {
    self.selectedItemString = [self packageSelectedNameString];
    UIColor *textColor;
    NSString *labelContent;
    if (_selectedItemString.length > 0) {
        labelContent = _selectedItemString;
//        textColor = [UIColor hexColor:FILTER_RED_STRING];
        textColor = Colorff0000;
    } else {
        labelContent = @"全部";
//        textColor = [UIColor hexColor:@"999999"];
        textColor = Color999999;
    }
    [self.subTitleL setText:labelContent];
    [self.subTitleL setTextColor:textColor];
}

- (void)tap2SelectItem:(NSIndexPath *)indexPath {
    switch (_selectionType) {
        case BrandTableViewCellSelectionTypeSingle:
        {
            NSArray *itemArray = _regionModel.itemList;
            CommonItemModel *model = [itemArray objectAtIndex:indexPath.row];
            if (!model.selected) {
                for (CommonItemModel *item in itemArray) {
                    item.selected = NO;
                }
                [self.selectedItemList removeAllObjects];
                [self.selectedItemList addObject:model];
            } else {
                [self.selectedItemList removeObject:model];
            }
            model.selected = !model.selected;
        }
            break;
        case BrandTableViewCellSelectionTypeMultiple:
        {
            NSArray *itemArray = _regionModel.itemList;
            CommonItemModel *model = [itemArray objectAtIndex:indexPath.row];
            model.selected = !model.selected;
            if (model.selected) {
                [self.selectedItemList addObject:model];
            } else {
                [self.selectedItemList removeObject:model];
            }
        }
            break;
        default:
            break;
    }
    _regionModel.selectedItemList = _selectedItemList;
    [self generateControlLabelText];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self configureCell];
    
    }
    
    return self;
}

#pragma mark -更多点击
- (void)subTitleClick:(UIGestureRecognizer *)recognizer {

    NSLog(@"加载更多");

    [self showMoreButton];
}

#pragma mark -箭头点击
- (void)arrowClick:(UIGestureRecognizer *)recognizer {

    NSLog(@"箭头点击加载更多");

    [self showMoreButton];

}

- (void)showMoreButton {
    
    _regionModel.isShowAll = !_regionModel.isShowAll;
    
    [self fitCollectonViewHeight];
    //reload
    if ([self.delegate respondsToSelector:@selector(sideSlipTableViewCellNeedsReload:)]) {
        [self.delegate sideSlipTableViewCellNeedsReload:_indexPath];
    }
    //scroll
    if (_regionModel.isShowAll && [self.delegate respondsToSelector:@selector(sideSlipTableViewCellNeedsScrollToCell:atScrollPosition:animated:)]) {
        [self.delegate sideSlipTableViewCellNeedsScrollToCell:self atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    }
    
    
}

- (void)configureCell {
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    [self.contentView addSubview:self.titleL];
    
    [self.contentView addSubview:self.subTitleL];
    [self.contentView addSubview:self.arrowIV];

    [self.contentView addSubview:self.mainCollectionView];

    self.titleL.frame = CGRectMake(jisuan(20), jisuan(30), jisuan(100), jisuan(36));
    self.arrowIV.frame = CGRectMake(jisuan(670) - jisuan(20) - jisuan(20), jisuan(38), jisuan(20), jisuan(20));
    self.subTitleL.frame = CGRectMake(jisuan(120) + jisuan(10), jisuan(30), jisuan(490), jisuan(36));
    
    self.mainCollectionView.frame = CGRectMake(0, jisuan(86), jisuan(670), jisuan(60));

}

#pragma mark -collectionView delegate \ datasource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.collectionArr.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    FilterCommonCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[FilterCommonCollectionViewCell cellReuseIdentifier] forIndexPath:indexPath];
    CommonItemModel *model = [self.collectionArr objectAtIndex:indexPath.row];
    [cell updateCellWithModel:model];
    return cell;
 
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    [self tap2SelectItem:indexPath];
    [_mainCollectionView reloadData];
    
}

#pragma mark -lazy

- (UICollectionView *)mainCollectionView {
    
    if (!_mainCollectionView) {
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.itemSize = CGSizeMake(jisuan(196), jisuan(60));
        flowLayout.minimumLineSpacing = jisuan(20);
        flowLayout.minimumInteritemSpacing = jisuan(20);
        flowLayout.sectionInset = UIEdgeInsetsMake(0, jisuan(20), jisuan(30), jisuan(20));
        
        _mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        
        _mainCollectionView.backgroundColor = WhiteColor;
        _mainCollectionView.delegate = self;
        _mainCollectionView.dataSource = self;
        
        _mainCollectionView.scrollEnabled = NO;
        
        _mainCollectionView.showsVerticalScrollIndicator = NO;
        _mainCollectionView.showsHorizontalScrollIndicator = NO;
        
        [_mainCollectionView registerClass:[FilterCommonCollectionViewCell class] forCellWithReuseIdentifier:[FilterCommonCollectionViewCell cellReuseIdentifier]];
        
        
        if (@available(iOS 11.0, *)) {
            _mainCollectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        
        }
        
    }
    
    return _mainCollectionView;
}
- (UILabel *)titleL {
    
    if (!_titleL) {
        
        _titleL = [[UILabel alloc] init];
        _titleL.font = Font(24);
        _titleL.textColor = Color333333;
        _titleL.textAlignment = NSTextAlignmentLeft;
        
    }
    return _titleL;
    
}
- (UILabel *)subTitleL {
    
    if (!_subTitleL) {
        
        _subTitleL = [[UILabel alloc] init];
        _subTitleL.font = Font(20);
        _subTitleL.textColor = Color999999;
        _subTitleL.textAlignment = NSTextAlignmentRight;
        _subTitleL.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *subTGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(subTitleClick:)];
        [_subTitleL addGestureRecognizer:subTGesture];
        
    }
    
    return _subTitleL;
    
}

- (NSMutableArray *)selectedItemList {
    if (!_selectedItemList) {
        _selectedItemList = [NSMutableArray array];
    }
    return _selectedItemList;
}

- (UIImageView *)arrowIV {
    
    if (!_arrowIV) {
        
        _arrowIV = [[UIImageView alloc] init];
        _arrowIV.contentMode = UIViewContentModeScaleAspectFit;
        _arrowIV.image = [UIImage imageNamed:@"sectionHeaderArrowRigthbg"];
        
        _arrowIV.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *arrowTGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(arrowClick:)];
        [_arrowIV addGestureRecognizer:arrowTGesture];
        
    }
    
    return _arrowIV;
    
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
